{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 移动字符串\n",
    "\n",
    "![Asked by Apple](https://img.shields.io/badge/Asked%20by-Apple-important?style=for-the-badge&logo=apple)\n",
    "\n",
    "&emsp;&emsp;给定两个字符串`A`和`B`，确定是否可以将`A`整体移动几次以获得`B`。\n",
    "\n",
    "&emsp;&emsp;例如：\n",
    "\n",
    "```text\n",
    " In[1]: A = 'abcde', B = 'cdeab'\n",
    "Out[1]: True\n",
    "\n",
    " In[2]: A = 'abc', B = 'acb'\n",
    "Out[2]: False\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isShifted1(a: str, b: str) -> bool:\n",
    "    if len(a) != len(b):\n",
    "        return False\n",
    "    length = len(a)\n",
    "    for i in range(length):\n",
    "        same = True\n",
    "        for j in range(length):\n",
    "            if a[(i + j) % length] != b[j]:\n",
    "                same = False\n",
    "                break\n",
    "        if same:\n",
    "            return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isShifted2(a: str, b: str) -> bool:\n",
    "    if len(a) != len(b):\n",
    "        return False\n",
    "    return b in (a + a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(isShifted1('abc', 'acb'))\n",
    "print(isShifted2('abcde', 'cdeab'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(isShifted1('abc', 'acb'))\n",
    "print(isShifted2('abcde', 'cdeab'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
